#include "MovingObject.h"

void MovingObject::setPosition(Vector3D pos)
{ 
	v3Position = pos;
	D3DXMatrixTranslation(&d3dPosition, v3Position.getX(), v3Position.getY(), v3Position.getZ());
}	

void MovingObject::setPosition(float x, float y, float z)
{ 
	v3Position.setCoord(x, y, z);
	D3DXMatrixTranslation(&d3dPosition, x, y, z); 
}

//void MovingObject::setTarget(float x, float y, float z)
//{ 
//	v3Target.setCoord(x, y, z); 
//	D3DXMatrixTranslation(&d3dTarget, v3Target.getX(), v3Target.getY(), v3Target.getZ());
//}
//
//void MovingObject::setTarget(Vector3D tar)
//{
//	v3Target = tar; 
//	D3DXMatrixTranslation(&d3dTarget, v3Target.getX(), v3Target.getY(), v3Target.getZ());
//}

void MovingObject::setAngle(float theta)
{ 
	fAngle = theta;
	D3DXMatrixRotationZ(&d3dPosition, theta);
}

void MovingObject::Update(float dt)
{
	//disallows angle to go above 2*pi or below 0
	if(fAngle < 0)
		fAngle += 2 * PI;
	else if(fAngle >= 2 * PI)
		fAngle -= 2 * PI;

	//rotates object to the desired angle while going through the position update
	if(fabs(fDesiredAngle - fAngle) > .1)
	{
		if(fDesiredAngle < PI)
			fAngle += .01f;
		else
			fAngle -= .01f;
		D3DXMatrixRotationZ(&d3dAngle, -1 * fAngle);
	}
		v3Velocity = v3Velocity + v3Accel * dt;
		v3Position = v3Position + v3Velocity * dt;

		//after finding the position in our Vector3D format, d3dPosition is given a translation matrix version of it
		D3DXMatrixTranslation(&d3dPosition, v3Position.getX(), v3Position.getY(), v3Position.getZ());

}

void MovingObject::Calculate()
{
	if(isSeeking)
		mySteering.Seek(mopSeekTarget);
	if(isFleeing)
		mySteering.Flee(mopFleeTarget);
	if(isEvading)
		mySteering.Evade(mopEvadeTarget);
	if(isPursuing)
		mySteering.Pursuit(mopPursuitTarget);
}

bool MovingObject::isAtTarget(Vector3D v3SteeringTarget)
{
	if(v3Position.DistanceTo(v3SteeringTarget) < .1)
		return true;
	
	return false;
}